package com.gkey.sole.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gkey.base.cache.PathParameter;
import com.gkey.base.cache.UsualParameter;
import com.gkey.base.web.BaseController;
import com.gkey.core.web.util.SessionUtil;
import com.gkey.sole.base.bean.CUser;
import com.gkey.sole.base.service.LoginService;
import com.gkey.sole.dict.bean.DPathParam;

/**  
 * @Title: 注册页面.java
 * @Package com.gkey.sole.base.controller
 * @Description: TODO
 * @author GuoKey
 * @date 2016年8月10日 下午6:11:32
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/register")
public class RegisterController extends BaseController<CUser, Integer> {
	
	
	protected Logger log = Logger.getLogger(RegisterController.class);
	
	@Resource
	protected LoginService loginService;
	
	
	/**
	 * @Description: 跳转到注册页面
	 * @param req
	 * @param res
	 * @param model
	 * @return String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月18日 下午5:14:23
	 */
	@RequestMapping
    public String index(HttpServletRequest req, HttpServletResponse res, Model model){		
		DPathParam param = PathParameter.AcquirePathModel(req.getRequestURL()+"");
    	req.getSession().setAttribute("params", param);		
    	return param.getWebprefixs() +"register";    	
    }
	
	
	/**
	 * 
	 * @Description: 检验注册名、验证码
	 * @param request
	 * @return   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月22日 下午5:30:04
	 */
    @RequestMapping(value = "/check")
    @ResponseBody
    public String check(HttpServletRequest request){
    	String sImageCode = (String) request.getSession().getAttribute("RANDOMVALIDATECODEKEY");
    	String loginName=request.getParameter("loginname"); 
    	String imageCode=request.getParameter("imageCode");
    	if(!sImageCode.toUpperCase().equals(imageCode.toUpperCase())){
            return "imageCodeError";
        }
    	CUser cuser=loginService.getCUser(loginName);
    	if(cuser!=null){
    		return "userError";
    	}    	
    	return "yes";
    }
	
    
    /**
     * 保存用户
     * @param user
     * @param model
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value="/saveuser")
    public String saveUser(CUser cuser,HttpServletRequest request,RedirectAttributes redirectAttributes,Model model){
    	
    	cuser.setState(10);;//正常状态,无须审核
    	
    	cuser.setUsertype(1);//rcp注册
    	
    	loginService.saveorupdate(cuser);
		
		log.debug("login:"+cuser.getLoginname()+"|"+cuser.getName());
    	
        SessionUtil.setSessionAttribute(request, UsualParameter.SESSION_USER, cuser);//login_user
        
        SessionUtil.setSessionAttribute(request, UsualParameter.SESSION_USERID, cuser.getId());//login_userid
        
		return "redirect:/welcome";
    }
    
    
    
	/**
	 * 
	 * @Description: 跳转协议页面
	 * @return   
	 * String  
	 * @throws
	 * @author keyGuo
	 * @date 2016年8月22日 下午5:38:42
	 */
    @RequestMapping(value = "/protocal")
    public String protocal(){
    	
    	return "proto/protocal";
    }
	

}
